﻿#include "EasySelectServer.h"
#include "CELLSelectServer.h"
#include <errno.h>

void EasySelectServer::Start(int nCELLServer)
{
	EasyTcpServer::Start<CELLSelectServer>(nCELLServer);
}

// 处理网络消息
void EasySelectServer::OnRun(CELLThread* pthread)
{
	long second;
	long microseconds;
	while (pthread->isRun())
	{
		time4msg();
		fd_set fdRead;
		FD_ZERO(&fdRead);

		FD_SET(sockfd(), &fdRead);
		time_t time = _time_event.getTime();

		second = time / 1000;
		microseconds = time % 1000 * 1000;
		struct timeval ti = { second, microseconds};
		int ret = select(sockfd() + 1, &fdRead, 0, 0, &ti);
		if (ret < 0)
		{
			if (errno == EINTR)
			{
				continue;
			}

			CELLLog_pError("EasyTcpServer OnRun select error");
			pthread->Exit();
			return;
		}
		if (FD_ISSET(sockfd(), &fdRead))
		{
			// 监听套接字有新连接
			Accept();
		}
		_time_event.process_timer();
	}
	CELLLog_Info("EasyTcpServer OnRun exit");
}
